package gui.win.proj.add;

import core.utils.Utils;
import gui.Segl1;
import gui.processor.Processor;
import gui.processor.Worker;
import gui.utils.Para;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class Buttons extends JPanel implements ActionListener{
    private JButton close;
    private JButton add;
    private AddP addP;
    private Segl1 segL1;
    public Buttons(Segl1 segL1, AddP addP){
        super(new BorderLayout());
        this.segL1 = segL1;
        this.addP = addP;
        this.add(Utils.getButton(close = new JButton("CANCEL"), 184, 22, new Color(102, 102, 102), new Color(178, 178, 178), this, "CANCEL"), BorderLayout.WEST);
        this.add(Utils.getButton(add = new JButton("ADD"), 184, 22, new Color(102, 102, 102), new Color(178, 178, 178), this, "ADD"), BorderLayout.EAST);
    }

    public void actionPerformed(ActionEvent evt) {
        Object source = evt.getSource();
        if(source == close){
            addP.closeFrame();
        }
        else if(source == add){
            if(addP.getPTitle().equals("") || addP.getPTitle()==null){
                JOptionPane.showMessageDialog(null, "Project title not entered.", "alert", JOptionPane.ERROR_MESSAGE);
            }
            else if(addP.getPath().equals("") || addP.getPath()==null){
                JOptionPane.showMessageDialog(null, "Path to templates not entered.", "alert", JOptionPane.ERROR_MESSAGE);
            }
            else{
                if(!new File(addP.getPath()).exists()){JOptionPane.showMessageDialog(null, "Template file does not exist.", "alert", JOptionPane.ERROR_MESSAGE);}
                else{
                    Para.P_TITLE = addP.getPTitle();
                    Para.PATH_TO_TEMPLATE = addP.getPath();
                    Processor processor = new Processor(segL1);
                    processor.setTask(Para.TASK_ADD_PROJ);
                    Worker sl = new Worker(processor);
                    sl.execute();
                    addP.closeFrame();
                }
            }
        }
    }
}